package com.tanhua.server.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * * @title: QuanZiCommentController
 *
 * @Author Philcan
 * @Date: 2021/8/12 19:37
 * @Version 1.0
 */
@RestController
@RequestMapping("/comments")
public class QuanZiCommentController {
    @Autowired
    private QuanZiService quanZiService;

    @GetMapping
    public ResponseEntity<PageResult> queryCommentList(@RequestParam("movementId") String publishId,
                                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = this.quanZiService.queryCommentList(publishId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping
    public ResponseEntity<Void> saveComment(@RequestBody Map<String, String> param) {
        try {
            String publishId = param.get("movementId");
            String comment = param.get("comment");
            Boolean result = this.quanZiService.saveComments(publishId, comment);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        try {
            Long comment = this.quanZiService.likeComment(publishId);
            if (comment != null) {
                return ResponseEntity.ok(comment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        try {
            Long disLikeComment = this.quanZiService.disLikeComment(publishId);
            if (null != disLikeComment) {
                return ResponseEntity.ok(disLikeComment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
